package com.cats.template.sys.controller;

import com.cats.template.sys.aggservices.SysAggService;
import com.cats.template.sys.pojo.dto.PermissionDto;
import com.cats.template.sys.pojo.dto.RoleDto;
import com.cats.template.sys.pojo.dto.UserDto;
import com.cats.template.sys.utils.ResponseVO;
import com.cats.template.sys.utils.UserInfoUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author duxiaobo
 * @date 2021/9/2610:52 上午
 */
@RestController
@RequiresAuthentication
@RequestMapping("sys")
public class SysController {

    @Autowired
    private SysAggService sysAggService;

    /**
     * 获取当前登录用户信息
     * @return
     */
    @GetMapping("/user/info")
    public ResponseVO<UserDto> getUserInfo() {
        return ResponseVO.success(UserInfoUtils.getCurrentUser());
    }

    /**
     * 保存用户
     * @param userDto
     * @return
     */
    @PostMapping("/user/save")
    @RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
    public ResponseVO<UserDto> saveUser(@RequestBody UserDto userDto) {
        return ResponseVO.success(sysAggService.saveUser(userDto));
    }

    /**
     * 通过用户Id获取用户信息
     * @param userId
     * @return
     */
    @GetMapping ("/user/get/{userId}")
    @RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
    public ResponseVO<UserDto> getUser(@PathVariable("userId") Long userId) {
        return ResponseVO.success(sysAggService.getUser(userId));
    }

    /**
     * 配置用户角色
     * @param userId
     * @param roleIds
     * @return
     */
    @PostMapping("/user/configRole/{userId}")
    @RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
    public ResponseVO<Boolean> configRoles(@PathVariable("userId") Long userId, @RequestBody Set<Long> roleIds) {
        return ResponseVO.success(sysAggService.configRoles(userId, roleIds));
    }

    /**
     * 保存角色
     * @param roleDto
     * @return
     */
    @PostMapping("/role/save")
    @RequiresRoles({"superAdmin"})
    public ResponseVO<RoleDto> saveRole(@RequestBody RoleDto roleDto) {
        return ResponseVO.success(sysAggService.saveRole(roleDto));
    }

    /**
     * 通过角色id获取角色信息
     * @param roleId
     * @return
     */
    @GetMapping("/role/get/{roleId}")
    @RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
    public ResponseVO<RoleDto> getRole(@PathVariable("roleId") Long roleId) {
        return ResponseVO.success(sysAggService.getRole(roleId));
    }

    /**
     * 给角色配置权限
     * @param roleId
     * @param permissionIds
     * @return
     */
    @PostMapping("/role/configPermissions/{roleId}")
    @RequiresRoles({"superAdmin"})
    public ResponseVO<Boolean> configPermissions(@PathVariable("roleId") Long roleId, @RequestBody Set<Long> permissionIds) {
        return ResponseVO.success(sysAggService.configPermissions(roleId, permissionIds));
    }

    /**
     * 保存权限
     * @param permissionDto
     * @return
     */
    @PostMapping("/permission/save")
    @RequiresRoles({"superAdmin"})
    public ResponseVO<PermissionDto> savePermission(@RequestBody PermissionDto permissionDto) {
        return ResponseVO.success(sysAggService.savePermission(permissionDto));
    }

    /**
     * 批量保存权限
     * @param permissionDtoSet
     * @return
     */
    @PostMapping("/permission/save/list")
    @RequiresRoles({"superAdmin"})
    public ResponseVO<PermissionDto> savePermissionSet(@RequestBody Set<PermissionDto> permissionDtoSet) {
        return ResponseVO.success(sysAggService.savePermission(permissionDtoSet));
    }

}
